package com.ebei.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ebei.admin.common.constant.Constants;
import com.ebei.admin.common.util.AuthRealm;
import com.ebei.admin.common.util.ConvertUtil;
import com.ebei.admin.mapper.SysCompanyMapper;
import com.ebei.admin.mapper.SysOrgMapper;
import com.ebei.admin.model.dto.OrgDTO;
import com.ebei.admin.model.entity.SysCompany;
import com.ebei.admin.model.entity.SysOrg;
import com.ebei.admin.model.entity.SysPark;
import com.ebei.admin.model.entity.SysUser;
import com.ebei.admin.service.SysCompanyService;
import com.ebei.admin.service.SysOrgService;
import com.ebei.admin.service.SysUserService;
import com.ebei.common.constant.CommonConstant;
import com.ebei.common.util.Query;
import com.ebei.common.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysCompanyServiceImpl extends ServiceImpl<SysCompanyMapper, SysCompany> implements SysCompanyService {

    @Autowired
    private SysCompanyMapper sysCompanyMapper;
    @Autowired
    private SysOrgMapper sysOrgMapper;
    @Autowired
    private SysOrgService sysOrgService;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public Page findCompanyListByParams(Query<Map<String, Object>> query) {
        Map<String, Object> map = query.getCondition();
        query.setRecords(sysCompanyMapper.findCompanyListByParams(query, map));
        return query;
    }

    @Override
    public Result<Boolean> update(Map<String, Object> map) throws Exception {
        SysCompany sysCompany = (SysCompany) ConvertUtil.mapToObject(map, SysCompany.class);
        sysCompany.setUpdateTime(new Date());
        sysCompanyMapper.updateById(sysCompany);
        SysOrg sysOrg = new SysOrg();
        BeanUtils.copyProperties(sysCompany, sysOrg);
        sysOrgMapper.updateById(sysOrg);
        return Result.OK;
    }

    @Override
    public Result findByUserId(Integer userId) {
        return new Result(sysCompanyMapper.findByUserId(userId), Result.SUCCESS);
    }

    @Override
    public Result delete(Integer id) {
        SysCompany sysCompany = sysCompanyMapper.selectById(id);
        sysCompany.setDelFlag(CommonConstant.STATUS_DEL);
        sysCompanyMapper.updateById(sysCompany);
        return Result.OK;
    }

    @Override
    public Result lock(OrgDTO orgDTO) {
        // 查询企业管理员账户
        SysCompany sysCompany = sysCompanyMapper.selectById(orgDTO.getOrgId());
        SysUser condition = new SysUser();
        condition.setUserAccount(sysCompany.getAdminAccount());
        condition.setDelFlag(CommonConstant.STATUS_NORMAL);
        SysUser adminUser;
        try {
            adminUser = sysUserService.selectOne(new EntityWrapper<>(condition));
        } catch (Exception e){
            return new Result(Result.FAIL,"数据错误");
        }

        // 锁定管理员账号
        adminUser.setDelFlag(orgDTO.getDelFlag());
        adminUser.setUpdateTime(new Date());
        sysUserService.updateById(adminUser);
        return new Result(Result.SUCCESS,"已禁用");
    }

    @Override
    public Result getCompanyJsonForYHPC(Integer parkId) {
        Map<String,Object> params = new HashMap<>();
        params.put("parkId",parkId);
        params.put("industrySector",Constants.INDUSTRY_SECTOR);
        List<Map<String,Object>> result =  sysCompanyMapper.getCompanyJsonForYHPC(params);
        return new Result(result,Result.SUCCESS,"获取成功");
    }

    @Override
    public Result<Page> getAllCompanyForDevice(Map<String, Object> params) {
        try {
            Query query = new Query(params);
            query.setRecords(sysCompanyMapper.getAllCompanyForDevice(query, query.getCondition()));
            return new Result(query, Result.SUCCESS, "获取成功");
        } catch (Exception e){
            e.printStackTrace();
            return new Result(Result.FAIL, "系统错误");
        }
    }
}
